package mybatis.test;
import mybatis.MyBatisUtils;
import mybatis.mapper.WorkerMapper;
import mybatis.pojo.Worker;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;
import java.util.List;

public class Part20_TestAnnotation {
    public static void findWorkerByIdTest(int id) {
        // 1.通过工具类获取SqlSession对象
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 2.使用WorkerMapper对象查询id为1的员工的信息
        Worker worker = mapper.selectWorker(id);
        System.out.println(worker.toString());
        // 3.关闭SqlSession
        session.close();
    }
    public static void insertWorkerTest(int worker_id) {
        // 1.通过工具类生成SqlSession对象
        SqlSession session = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setName("赵六");
        worker.setAge(36);
        worker.setSex("女");
        worker.setWorker_id(worker_id);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 2.插入员工信息
        int result = mapper.insertWorker(worker);
        if (result > 0) {
            System.out.println("成功插入" + result + "条数据");
        } else {
            System.out.println("插入数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        // 3.关闭SqlSession
        session.close();
    }

    public static void updateWorkerTest(int id) {
        // 1.通过工具类生成SqlSession对象
        SqlSession session = MyBatisUtils.getSession();
        Worker worker = new Worker();
        worker.setId(id);
        worker.setName("李华");
        worker.setAge(28);
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 2.更新员工信息
        int result = mapper.updateWorker(worker);
        if (result > 0) {
            System.out.println("成功更新" + result + "条数据");
        } else {
            System.out.println("更新数据失败");
        }
        System.out.println(worker.toString());
        session.commit();
        // 3.关闭SqlSession
        session.close();
    }


    public static void deleteWorkerTest(int id) {
        // 1.通过工具类生成SqlSession对象
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        // 2.删除员工信息
        int result = mapper.deleteWorker(id);
        if (result > 0) {
            System.out.println("成功删除" + result + "条数据");
        } else {
            System.out.println("删除数据失败");
        }
        session.commit();
        // 3.关闭SqlSession
        session.close();
    }

    public static void deleteWorkerTest() {
        SqlSession session = MyBatisUtils.getSession();
        WorkerMapper mapper = session.getMapper(WorkerMapper.class);
        List<Worker> workers = new ArrayList<>();
        for (int i = 1; i < 5; i++) {
            workers.add(mapper.selectWorker(i));
        }
        System.out.println(workers);
        mapper.deleteWorkers(workers);
        session.commit();

    }

    public static void main(String[] args){
        deleteWorkerTest();
//        findWorkerByIdTest(4);
//        updateWorkerTest();
//        deleteWorkerTest();
    }
}

